package com.wmm.fast.main.shop2Observer.eventBus;

import lombok.Data;

import java.lang.reflect.Method;

/**
 * @Auther: wangzun
 * @Date: 2018/9/13 10:56
 * @Description: 具体事件对象,事件包含
 */
@Data
public class Event {
    private Object bean;
    private String methodName;
    private Object[] params;
    private Class[] paramsTypes;

    public Event(Object bean,String methodName,Object[] params) {
        this.bean = bean;
        this.methodName = methodName;
        this.params = params;
        concrateParamTypes(params);
    }

    /** 根据参数生成参数类型数组 */
    private void concrateParamTypes(Object[] params) {
        this.paramsTypes = new Class[params.length];
        for (int i = 0; i < params.length; i++) {
            this.paramsTypes[i] = params[i].getClass();
        }
    }

    /** invoke */
    public void invoke() throws Exception {
        Method method = bean.getClass().getMethod(this.getMethodName(), this.getParamsTypes());
        if(null == method) {
            return;
        }
        method.invoke(this.getBean(), this.getParams());
    }
}
